package com.example.demo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;



/*
 * 当Servlet抛出异常
 *
 * */


/*
 *javax.servlet.error.status_code
 *javax.servlet.error.exception_type
 *javax.servlet.error.message
 *javax.servlet.error.request_uri
 *javax.servlet.error.exception
 *javax.servlet.error.servlet_name
 * */
@WebServlet("/errorHandler")
public class ErrorHandler extends HttpServlet {


    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
        Integer statusCode = (Integer) req.getAttribute("javax.servlet.error.status_code");
        String servletName = (String) req.getAttribute("javax.servlet.error.message");
        if (servletName == null) {
            servletName = "unKnown";
        }
        String requestUri = (String) req.getAttribute("javax.servlet.error.request_uri");
        if (requestUri == null) {
            requestUri = "unKnown";
        }

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out= resp.getWriter();

        out.println("status   "+statusCode);
        out.println("servletName   "+servletName);
        out.println("requestUri   "+requestUri);


    }
}
